<?php
class EWebby_HttpProfile implements EWebby_IPersist {
	private static $_SESSION_NAME = '__PROFILE__';
	public static $timeOut = 90;	// minute

	public $roles = null;
	
	public static function getInstance() {
		self::_startSession();
		
		return ( !isset($_SESSION [self::$_SESSION_NAME]) )?
			null : $_SESSION [self::$_SESSION_NAME];
	}
	
	public function save() {
		$_SESSION [self::$_SESSION_NAME] = EWebby_WebApplication::$current->profile = $this;
	}
	public function clear() {
		if( !isset($_SESSION[self::$_SESSION_NAME]) ) {
			return;
		}

		unset($_SESSION[self::$_SESSION_NAME]);
		$_SESSION[self::$_SESSION_NAME] = EWebby_WebApplication::$current->profile = null;
		session_destroy();
	}
	
	private static function _startSession() {
		@ini_set('session.gc_maxlifetime', self::$timeOut * 60);	/* set the session timeout (in seconds)  */
		session_cache_expire( self::$timeOut );					/* expiretime is the lifetime in minutes */
		
		session_start();	// start the session
	}
	
	public function containsRole($roleName) {
		if( $this->roles != null 
			&& is_array($this->roles) ) {
			foreach ($this->roles as $role) {
				if( $role == $roleName ) {
					return true;
				}
			}
		}
		
		return false;
	}
}